package design.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 简单工厂的本质是选择实现
 */
public class Factory {

    public static Api createApi(){
        //直接读取配置文件，获取需要创建的实例的类
        Properties p = new Properties();
        InputStream in = null;
        try {
            in = Factory.class.getResourceAsStream("FactoryTest.properties");
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Api api = null;
        try {
            api = (Api) Class.forName(p.getProperty("ImplClass")).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return api;
    }

}
